package com.vict.framework.pay.utils;

import com.vict.framework.pay.bean.PayTaskDTO;
import com.vict.framework.pay.entity.PayOrder;
import com.vict.framework.pay.service.FramworkPayInnerService;
import com.vict.framework.pay.taskimpl.PayTask;
import com.vict.framework.task.util.TaskExecuteUtil;
import com.vict.framework.utils.IdUtils;
import com.vict.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

@Slf4j
@Component
public class PayUtils {

    private static FramworkPayInnerService framworkPayInnerService;

    @Autowired
    public void setPayInnerService(FramworkPayInnerService framworkPayInnerService){
        PayUtils.framworkPayInnerService = framworkPayInnerService;
    }


    /** 创建支付id */
    public static String createPayDtoId(){
        long lId = IdUtils.snowflakeId();
        String id = "" + TimeUtil.getTimeStr("yyyyMMdd", System.currentTimeMillis()) + lId + "";
        log.info("生成支付id {}", id);
        return id;
    }

    /** 创建退款id */
    public static String createPayBackDtoId(){
        long lId = IdUtils.snowflakeId();
        String id = TimeUtil.getTimeStr("yyyyMMdd", System.currentTimeMillis()) + lId + "";
        id = "back-" + id;
        log.info("生成退款id {}", id);
        return id;
    }

    /** 支付完成 */
    public static void payFinish(String payDtoId, PayOrder.PayType payType){
        long now = System.currentTimeMillis();

        PayTaskDTO payTaskDTO = new PayTaskDTO();
        payTaskDTO.setPayDtoId(payDtoId);
        payTaskDTO.setSuccessTime(new Timestamp(now));
        payTaskDTO.setPayType(payType);

        TaskExecuteUtil.sendTask(PayTask.type, payTaskDTO, new Timestamp(System.currentTimeMillis()));
    }

    /** 取消 */
    public static void payCancel(String payDtoId){
        framworkPayInnerService.payCancel(payDtoId);
    }

    /** 退款 */
    public static void payBack(String payDtoId, String payBackDtoId, BigDecimal backMoney){
        framworkPayInnerService.payBack(payDtoId, payBackDtoId, backMoney);
    }
}
